/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Chatree Siangklom
 * Created On : Dec 20, 2012  4:48:06 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : com.tis.std.batch.sales
 * File Name : SalesBankRequestBatch.java
 * TODO
 */
package com.tis.std.batch.sales;

import java.util.List;

import org.apache.log4j.Logger;

import promis.utility.CommonService;
import promis.utility.QueueProcessAction;
import apps.sessionFactory;

import com.tis.std.commons;

/**
 * @author chatree
 *
 */
public class SalesBankRequestBatch extends commons {
	
	private static final Logger log = Logger.getLogger(SalesBankRequestBatch.class);
	
	private void BatchExec() throws Exception {
		try{
			
			StringBuilder _strQuery = new StringBuilder();

			_strQuery.append("  ");
			_strQuery.append(" SELECT  ");
			_strQuery.append(" COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON, CONTRACT_NO "); 
			_strQuery.append(" FROM SPL.VW_SYNC_MKT_BANK_DEDU_REQU   ");
			_strQuery.append(" WHERE SYNC_ISSTAGING = 'N'  ");
			_strQuery.append(" AND STATUS IN ('A','Z') ");
			_strQuery.append(" AND APPROVED_BY IS NOT NULL ");
			_strQuery.append(" GROUP BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON, CONTRACT_NO "); 
			_strQuery.append(" ORDER BY COMPANY, PROJECT, DOCUMENT_NO, STATUS, SYNC_ISSTAGING, CREATED_BY, CREATED_ON, APPROVED_BY, APPROVED_ON, CANCELLED_BY, CANCELLED_ON, CONTRACT_NO ");
			
			List<Object[]> result = (List<Object[]>) sessionFactory.CreateEmPromis().createNativeQuery(_strQuery.toString()).getResultList();
			System.out.println("result size =" + CommonService.getSize(result));
			
			if(!CommonService.IsNull(result)){
				try{
					for (Object[] value : result) {	
						CleanValue();
						setAction(promis.utility.InterfaceAction.SalesContract.PER_DOC.toString());
						setCompany(CommonService.IsNullRNull(value[0]));
						setProject(CommonService.IsNullRNull(value[1]));
						setContractNo(CommonService.IsNullRNull(value[11]));
						setStatus(CommonService.IsNullRNull(value[3]));
						setSyncIsstaging(CommonService.IsNullRNull(value[4]));
						setProcessAction(QueueProcessAction.BATCH);
						setProcessActionBy(CommonService.IsNullRNull(value[7]));
						
						
						new apps.promis.out.common.SyncQueueAction().PutQueue(	promis.utility.BusinessModule.SALES.toString(), 
																				promis.utility.ProcessModule.SALES_BANK_REQUEST.toString(),
																				null,
																				QueueProcessAction.BATCH,
																				ConvertParamToHashMap()
																				);
					}
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}catch(Exception e){
			log.error(e.getMessage());
			e.printStackTrace();
			throw e;
		}
	}
	
	public static void main(String[] args) {
		try{
			
			new SalesBankRequestBatch().BatchExec();
			
		}catch(Exception e){
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}

}
